const userController = require('../../controller/user.controller')
const tokenMiddleware = require('../../middleware/token.Middleware')
const resultFormatMiddleware = require('../../middleware/resultFormat.Middleware')

class UserRouter {
    constructor(router) {
        router.post('/user/:username', async function (req, res) {
            let {username} = req.params

            let [raw, filed] = await userController.findUserByName(username)

            if (raw.length < 1) {
                return resultFormatMiddleware.fail(res,'404',404,raw)
            }

            req.session.userinfo = tokenMiddleware.sign(raw[0])
            //签名后的token
            return resultFormatMiddleware.success(res, 'ok', 200, [req.session.userinfo])
        })

        router.post('/users',tokenMiddleware.verifyMiddleware, async function (req, res) {
            let [raw] = await userController.findAllUser()
            return res.json(raw)
        })
    }
}

module.exports = UserRouter
